/**
 * v-restrict-to-number:positive-integer：只允许输入正整数
 * v-restrict-to-number:positive-integer-has-zero：正整数和0
   v-restrict-to-number:integer：只允许输入整数
   v-restrict-to-number:positive-number：只允许输入正数
   v-restrict-to-number：允许输入任意合法数字
 */
export default {
  mounted(el: any, binding:any) {
    const type = binding.arg
    const targetNumber = binding.value ? parseFloat(binding.value) : null

    // function handleInput(event) {
    //   restrictInputToNumber(type, targetNumber, event)
    // }
    // el.addEventListener('input', handleInput)
    // el.__handleInput = handleInput
    function handleInput(event: HTMLElement) {
      if (!el.__composing) {
        restrictInputToNumber(type, targetNumber, event)
      }
    }

    function handleCompositionStart() {
      el.__composing = true
    }

    function handleCompositionEnd(event: HTMLElement) {
      el.__composing = false
      restrictInputToNumber(type, targetNumber, event)
    }

    el.addEventListener('input', handleInput)
    el.addEventListener('compositionstart', handleCompositionStart)
    el.addEventListener('compositionend', handleCompositionEnd)

    el.__handleInput = handleInput
    el.__handleCompositionStart = handleCompositionStart
    el.__handleCompositionEnd = handleCompositionEnd
  },
  unmounted(el: any) {
    // el.removeEventListener('input', el.__handleInput)
    // delete el.__handleInput
    el.removeEventListener('input', el.__handleInput)
    el.removeEventListener('compositionstart', el.__handleCompositionStart)
    el.removeEventListener('compositionend', el.__handleCompositionEnd)
    delete el.__handleInput
    delete el.__handleCompositionStart
    delete el.__handleCompositionEnd
  }
}
function restrictInputToNumber(type: String, targetNumber:any, event:any) {
  const input = event.target
  let value = input.value || ''
  let dotIndex:number
  let hasDot = false
  // 根据限制类型执行相应的逻辑
  switch (type) {
    case 'positive-integer-has-zero': // 支持正整数和0
      if (value != '0') {
        value = value.replace(/[a-zA-Z]/g, '')
        value = value.replace(/\D/g, '') // 正整数，移除非数字字符
        value = value.replace(/^0+/, '') // 移除前导零
        value = value.replace(/[^0-9]/g, '') // 移除非法字符

        // 限制多个小数点和多个符号
        value = value.replace(/\./g, (match:any, offset:number) => {
          return offset === 0 ? '.' : ''
        })
        value = value.replace(/-/g, (match:any, offset:number) => {
          return offset === 0 ? '-' : ''
        })
      }
      break
    case 'positive-integer-nozero':
      value = value.replace(/[a-zA-Z]/g, '')
      value = value.replace(/\D/g, '') // 正整数，移除非数字字符
      value = value.replace(/^0+/, '') // 移除前导零
      value = value.replace(/[^0-9]/g, '') // 移除非法字符

      // 限制多个小数点和多个符号
      value = value.replace(/\./g, (match:any, offset:number) => {
        return offset === 0 ? '.' : ''
      })
      value = value.replace(/-/g, (match:any, offset:number) => {
        return offset === 0 ? '-' : ''
      })
      break
    case 'positive-integer':
      value = value.replace(/[a-zA-Z]/g, '')
      value = value.replace(/\D/g, '') // 正整数，移除非数字字符
      // value = value.replace(/^0+/, '') // 移除前导零 为了处理外国客户手机号0开头问题注释
      value = value.replace(/[^0-9]/g, '') // 移除非法字符

      // 限制多个小数点和多个符号
      value = value.replace(/\./g, (match:any, offset:number) => {
        return offset === 0 ? '.' : ''
      })
      value = value.replace(/-/g, (match:any, offset:number) => {
        return offset === 0 ? '-' : ''
      })
      break
    case 'integer':
      value = value.replace(/[a-zA-Z]/g, '')
      value = value.replace(/[^-0-9]/g, '') // 整数，移除非数字和负号字符
      // 限制多个小数点和多个符号
      value = value.replace(/\./g, (match:any, offset:number) => {
        return offset === 0 ? '.' : ''
      })
      value = value.replace(/-/g, (match:any, offset:number) => {
        return offset === 0 ? '-' : ''
      })
      break
    case 'positive-number':
      value = value.replace(/[a-zA-Z]/g, '')
      value = value.replace(/[^\d.]/g, '') // 正数，移除非数字和小数点字符
      value = value.replace(/^\./g, '') // 移除前导小数点
      value = value.replace(/\.{2,}/g, '.') // 多个小数点合并为一个
      value = value.replace(/^0(\d+)/, '$1') // 移除前导零

      // 限制多个符号
      hasDot = false // 用于标记是否已经出现过小数点
      value = value.replace(/-/g, (match:any, offset:Number) => {
        if (offset === 0) {
          return match
        } else {
          return ''
        }
      }).replace(/\./g, (match:any) => {
        if (!hasDot) {
          hasDot = true
          return match
        } else {
          return ''
        }
      })

      // 限制小数点最多两位
      dotIndex = value.indexOf('.')
      if (dotIndex !== -1) {
        const decimalPart = value.substr(dotIndex + 1)
        if (decimalPart.length > 2) {
          value = value.slice(0, dotIndex + 3)
        }
      }

      break
    default:
      value = value.replace(/[a-zA-Z]/g, '')
      value = value.replace(/[^-?\d.]/g, '')
      // 移除多个符号和多个小数点
      value = value.replace(/([-.])(?=.*\1)/g, '')
      break
  }
  if (value !== input.value) {
    // 检查新值是否与旧值不同
    input.value = value
    input.dispatchEvent(new Event('input'))
  }

  if (targetNumber !== null) {
    const numericValue = parseFloat(value)
    if (!isNaN(numericValue) && numericValue > targetNumber) {
      console.log(`The input number ${numericValue} is greater than ${targetNumber}.`)
      if (input.value !== targetNumber.toString()) {
        input.value = targetNumber
        input.dispatchEvent(new Event('input'))
      }
    }
  }
}
